﻿
var g_page_total = 100;					//总行数
var g_current_page_index = 1;  			//当前页码
var g_per_page_count = 10;				//每页几行
var g_a_count = 5;						//显示页码个数,超过5页用 >>表示

var g_tbody_obj	= null;					//tbody对象
var g_paging_container_obj = null;		//放置页脚的容器对象

var g_config_obj = null;        //配置对象

function PagingConfig()
{    
    this.command_page_1 = "paging";
    this.command_type_1 = "select_total_count";
    this.request_data_1 = ""; 
    
    this.command_page_2 = "paging";
    this.command_type_2 = "query_paging";
    this.request_data_2 = ""; 
}

//表名, sql语句返回字段(如"id, type,")
function SetCommonPagingConfig(tableName, selectParms)
{   
    if(g_config_obj == null)
        g_config_obj = new PagingConfig();
            
    g_config_obj.command_page_1 = "paging";
    g_config_obj.command_type_1 = "select_total_count";
    g_config_obj.request_data_1 = "&table_name=" + tableName; 
    
    g_config_obj.command_page_2 = "paging";
    g_config_obj.command_type_2 = "query_paging";
    g_config_obj.request_data_2 = "&table_name=" + tableName + "&select_parms=" + selectParms; 
} 

function SetPagingConfig(page1, type1, data1, page2, type2, data2, bDirectQueryData)
{   
    if(g_config_obj == null)
        g_config_obj = new PagingConfig();
            
    g_config_obj.command_page_1 = page1;
    g_config_obj.command_type_1 = type1;
    g_config_obj.request_data_1 = data1; 
    
    g_config_obj.command_page_2 = page2;
    g_config_obj.command_type_2 = type2;
    g_config_obj.request_data_2 = data2; 
	
	if(bDirectQueryData == null)
		bDirectQueryData = false;
	g_config_obj.bDirectQueryData = bDirectQueryData; //是否直接查询数据
} 

//*******分页*(每页行数，tbodyID, 页码容器ID, 分页配置)**********
function PagingInit(per_count, tbodyID, pagingContainerID, pagingConfigObj)
{
	g_per_page_count = per_count;
	g_tbody_obj = $(tbodyID);
	g_paging_container_obj = $(pagingContainerID); 
	if(g_tbody_obj == null || g_paging_container_obj == null) return;

    if(pagingConfigObj != null) g_config_obj = pagingConfigObj;
    
    g_current_page_index = 1; //初始化当前页码
    
	if(g_config_obj.bDirectQueryData == false) //先查总数, 再查某页数据
		RePaging(); 
	else __QueryInfo(0);      	//直接查询数据 
}
//-----重新查询当前页----
function RePaging()
{ 
    __RemoveAllChilds(g_tbody_obj); //先清空行
    __RemoveAllChilds(g_paging_container_obj); //先清空行  
	__QueryTotalCount();  //获取总行数
}

 //***********获取总行数***********
function __QueryTotalCount()
{ 
    var commandPage = g_config_obj.command_page_1;  
    var commandType = g_config_obj.command_type_1; 
    var _data = g_config_obj.request_data_1;  
         
    PostToPHP(__QueryTotalCountCallback, commandPage, commandType, _data);
}
//*********callback**
function __QueryTotalCountCallback(text)
{ 
    //alert(text);
	var jsonObj = String2JSON(text);
	var ds = GetDataSet(jsonObj);
	if(ds == null || ds[0]==null) return;

	g_page_total = ds[0]["total_count"];  //获取总行数
 
 	if(g_page_total == 0)
	{
		__DrawEmptyRow(g_per_page_count, g_ItemConfigArray.length);//空行
		return;
	}
	 var iPageCount = __GetPageCount();  //总页数
     if(g_current_page_index > iPageCount)
        g_current_page_index = iPageCount;
        
	__ReDrawPageFoot(g_current_page_index);		//画页码
	__QueryInfo(g_current_page_index);      	//查询当前页
}
 //---计算总页数
 function __GetPageCount()
 {    
	var iPageCount = parseInt(g_page_total/g_per_page_count);  //总页数(总行数/每页行数)
	if(g_page_total % g_per_page_count){ iPageCount += 1;}     //有余数，总页数+1
    return iPageCount;
 }
 
//----------重画页码
function __ReDrawPageFoot(currentPageIndex)
{
	__RemoveAllChilds(g_paging_container_obj); //先删除页码
 
    var iPageCount = __GetPageCount();  //总页数

	var iBook = parseInt((currentPageIndex-1) / g_a_count);   //第几排页脚(从0开始)

	var iBeginIndex = parseInt((iBook*g_a_count)+1);       //页脚开始序号
	var iEndIndex = parseInt((iBook+1)*g_a_count);         //页脚结束序号
 
	if(iBook > 0) //页脚大于0, 画"上一页"页脚
	{   __DrawA(g_paging_container_obj, "__OnClickForAPre", (iBeginIndex-1), " [<<]");
		__DrawEmptyNode(g_paging_container_obj); 
	}

	if(iPageCount < iEndIndex) //总页数 < 页脚结束序号
	{  
		for(var i=iBeginIndex; i<=iPageCount; i++)
		{ 	__DrawA(g_paging_container_obj, "__OnClickForA", i, (" ["+i+"]"));
			__DrawEmptyNode(g_paging_container_obj); 
		}
	}

	if(iPageCount >= iEndIndex) //总页数 >= 页脚结束序号
	{
		for(var i=iBeginIndex; i<=iEndIndex; i++)
		{ 	__DrawA(g_paging_container_obj, "__OnClickForA", i, (" ["+i+"]"));
			__DrawEmptyNode(g_paging_container_obj); 
		}
        
        if(iPageCount > iEndIndex) //总页数 > 页脚结束序号
		  __DrawA(g_paging_container_obj, "__OnClickForANext", (iEndIndex+1), " [>>]");
	}  
	__DrawCurrentPage(g_paging_container_obj, g_current_page_index); //当前页
}
//******当前页
function __DrawCurrentPage(pageContainerObj, index)
{ 
	var tx = document.createTextNode("    当前页["+ index + "]");
	pageContainerObj.appendChild(tx); 
}

//*******************页码点击方法**************
//[1] [2] [3]
function __OnClickForA(index)
{
	g_current_page_index = index; 
	__ReDrawPageFoot(index);
	__QueryInfo(index);
}
//>>>>
function __OnClickForANext(index)
{
	g_current_page_index = index; 
	__ReDrawPageFoot(index);
	__QueryInfo(index);
}
//<<<<
function __OnClickForAPre(index)
{
	g_current_page_index = index; 
	__ReDrawPageFoot(index);
	__QueryInfo(index);
} 

//**********************查询某页数据***********
function __QueryInfo(page_index)
{
	__RemoveAllChilds(g_tbody_obj)
 
    var commandPage = g_config_obj.command_page_2;
    var commandType = g_config_obj.command_type_2;
    
	 var _data = "&page_index=" + page_index;           //第几页
	_data += "&per_page_count=" + g_per_page_count;    //每页数量 
    _data += g_config_obj.request_data_2;
    
    PostToPHP(__QueryInfoCallback, commandPage, commandType, _data);

}
//-----
function __QueryInfoCallback(text)
{ 
	__DrawTbody(text);
}

//**********************画表格**********************
function __DrawTbody(text)
{ 
    //alert(text);
	var jsonObj = String2JSON(text);
	var ds = GetDataSet(jsonObj);
	if(ds == null) return;

	var iIndex = 0;
	for(var item in ds)
	{
		__DrawRow(ds[item], iIndex);
		iIndex++;
	} 
    //数据库查询的行数 少于 每页行数， 则填充空行
    var iRowLength = ds.length;
    if(iRowLength < g_per_page_count)
    {
        __DrawEmptyRow(g_per_page_count-iRowLength, g_ItemConfigArray.length);
    }
}
//************画tr
function __DrawRow(dbRow, index)
{
	var tr = document.createElement("tr");
	g_tbody_obj.appendChild(tr);

	for(var iconfig in g_ItemConfigArray)
	{
		 __DrawItem(tr, dbRow, g_ItemConfigArray[iconfig], index);
	}
}
//***********画td
function __DrawItem(trObj, dbRow, config, index)
{
 	var td = document.createElement("td");
 	var dbValue = dbRow[config.name];

	if(config.type == 0)
	{
	   if(dbValue == null || dbValue == "null") dbValue = "";
	   var maxLength = 30
	   if(dbValue.length && dbValue.length > maxLength)
	   {
			dbValue = dbValue.substr(0, maxLength)
			dbValue += "...[省略]"
	   }	
		var tx = document.createTextNode(dbValue);
		td.appendChild(tx);
	}
	else if(config.type == 1)
	{
		var par = index + ",'" + dbValue + "'";
		__DrawA(td, config.func, par, dbValue);
	}	
	else if(config.type == 2)
	{
		var par = index + ",'" + dbValue + "'";
		__DrawA(td, config.func, par, config.exdata);
	}
    else if(config.type == 3)
    {
    	var tx = document.createTextNode(config.name);
    	td.appendChild(tx);
    }	 
	
//--字段1---97----方法名(index,字段1, ex2)--显示文字----ex2--------------<a>显示文字</a>
	else if(config.type == 97)
	{
		var par = index + ", '" + dbValue + "', '" + dbRow[config.exdata2] + "'";
		__DrawA(td, config.func, par, config.exdata);
	}
	else if(config.type == 98)
	{
		var tmpTxt = "";
		if(config.func)
		{
			tmpTxt = config.func(index, dbValue);
		}
		var tx = document.createTextNode(tmpTxt);
		td.appendChild(tx);
	} 
	else if(config.type == 99)
	{        
        var f1 = dbValue;           //字段1
        var f2 = config.func;		//字段2
        var ex1 = config.exdata;    //扩展1
        
        var strHref = f2 + f1;
        var txt = ex1;   
		__DrawA2(td, strHref, txt);
	}	
	else if(config.type == 199)
	{        
        var f1 = dbValue;           //字段1
        var f2 = config.func;		//字段2
        var ex1 = config.exdata;    //扩展1
        var strHref = f2 + f1;
        var txt = dbRow[ex1];   
		__DrawA2(td, strHref, txt);
	}
    else if(config.type == 100)  //家具查询使用
	{        
        var f1 = dbValue;           //字段1
        var f2 = dbRow[config.func];//字段2
        var ex1 = config.exdata;    //扩展1
        var ex2 = config.exdata2;   //扩展2
        
        var strHref = ex1 + f1 + f2 + ex2;
        var txt = f2 + ex2 ;   
		__DrawA2(td, strHref, txt);
	} 
    else if(config.type == 101)  //装扮查询使用
	{        
        var f1 = dbValue;           //字段1
        var f2 = dbRow[config.func];//字段2
        var ex1 = config.exdata;    //扩展1
        var ex2 = config.exdata2;   //扩展2
        var ex3 = dbRow[config.exdata3];   //扩展3
        
        var strHref = ex1 + f1 + f2 + "_" + ex3 + ex2 ;
        var txt = f2 + "_" + ex3 + ex2  ;   
		__DrawA2(td, strHref, txt);
	}
    else if(config.type == 201)  
	{        
        var f1 = dbValue;           //字段1
        var f2 = config.func;		//字段2
        var ex1 = config.exdata;    //扩展1
        var ex2 = config.exdata2;   //扩展2
        var ex3 = config.exdata3;   //扩展3
        
        var txtID = dbValue + "_" + ex1; 
        var txtName = dbValue + "_" + ex1;
		var txtVal = dbRow[f2];
		__DrawInput(td, txtID, txtName, txtVal)
	}
    else if(config.type == 211)  
	{        
        var f1 = dbValue;           //字段1
        var f2 = config.func;		//字段2
        var ex1 = config.exdata;    //扩展1
        var ex2 = config.exdata2;   //扩展2
        var ex3 = config.exdata3;   //扩展3
        
        var txtID = dbValue + "_" + ex1; 
        var txtName = dbValue + "_" + ex1;
		var txtVal = dbRow[f2];
		var txtVal2 = dbRow[ex2];
		__DrawInput2(td, txtID, txtName, txtVal, txtVal2)
	}
    else if(config.type == 202)  
	{        
        var f1 = dbValue;           //字段1
        var f2 = config.func;		//字段2
        var ex1 = config.exdata;    //扩展1
        var ex2 = config.exdata2;   //扩展2
        var ex3 = config.exdata3;   //扩展3 
		
        var txtID = dbValue + "_" + ex1; 
        var txtName = dbValue + "_" + ex1;
		var txtVal = f2;
		__DrawCheckbox(td, txtID, txtName, txtVal, ex2)
	} 
	
	else if(config.type == 298)  //家具查询使用
	{        
        var f1 = dbValue;           //字段1
        var f2 = config.func;       //字段2
        var ex1 = config.exdata;    //扩展1
        var ex2 = config.exdata2;   //扩展2
        var ex3 = config.exdata3;   //扩展2
        
		var v1 = dbValue;   
		var v2 = dbRow[ex1];   
		if(v2)
		{
			var v3 = dbRow[ex2];
			var txt = ex3;   
			
			var strHref = "";
			if(config.func)
				strHref = config.func(index, v1, v2, v3); 
			__DrawA2(td, strHref, txt);
		}
	} 
//--字段1--298-----方法对象A(index,字段1值,字段2值,字段3值)---字段2----------字段3----exdata3---<a href="显示方法A返回的字符串">exdata3</a>

 	trObj.appendChild(td);
}
//***********画<a> --(容器对象, 点击方法名, 方法参数, 显示文字)--- 
function __DrawA(containObj, funcName, funcParm, txtVal)
{
	var aObj = document.createElement("a");
	aObj.href="javascript:"+ funcName +"(" + funcParm + ")";
	var tx = document.createTextNode(txtVal);
	aObj.appendChild(tx);
	containObj.appendChild(aObj);
}
//***********画<a>2 --(容器对象, 链接, 显示文字)--- 
function __DrawA2(containObj, hrefVal, txtVal)
{       
	var aObj = document.createElement("a");
	aObj.href= hrefVal; 
    
	var tx = document.createTextNode(txtVal);
	aObj.appendChild(tx);
    
    aObj.target = "_blank";  
	containObj.appendChild(aObj);
}

//***********画<input /> --(容器对象, 链接, 显示文字)--- 
function __DrawInput(containObj, txtID, txtName, txtVal)
{       
	var aObj = document.createElement("input");
	aObj.id = txtID; 
	aObj.name = txtName;
	aObj.value = txtVal ? txtVal : "";
	aObj.style.width = 120;  //style="width:120px"
	containObj.appendChild(aObj);
	
	//alert(aObj.id);
}
function __DrawInput2(containObj, txtID, txtName, txtVal, txtVal2)
{       
	var aObj = document.createElement("input");
	aObj.id = txtID; 
	aObj.name = txtName;
	aObj.value = txtVal ? txtVal : txtVal2;  //alert(txtVal2);
	aObj.style.width = 120;  //style="width:120px"
	containObj.appendChild(aObj);
	
	//alert(aObj.id);
}

function __DrawCheckbox(containObj, txtID, txtName, txtVal, ex2)
{       
	var aObj = document.createElement("input");
	aObj.type = "checkbox";
	aObj.id = txtID; 
	aObj.name = txtName;
	aObj.value = txtVal;
	aObj.style.width = 20;  //style="width:120px"
	containObj.appendChild(aObj);
	
	var tx = document.createTextNode(ex2); 
	containObj.appendChild(tx);
}

function __DrawEmptyNode(containObj)
{ 
	var tx = document.createTextNode("    ");
	containObj.appendChild(tx); 
}

//**********************画空行***********
function __DrawEmptyRow(iCount, iItem)
{
	for(var i=0; i<iCount; i++)
	{
		var tr = document.createElement("tr");
		for(var j=0; j<iItem; j++)
		{
			var td = document.createElement("td");
			var tx = document.createTextNode("");
			td.appendChild(tx);
			tr.appendChild(td);
		}
		g_tbody_obj.appendChild(tr);
	}
}

//**********************删除所有子对象***********
function __RemoveAllChilds(obj)
{
	if(obj && obj.childNodes)
	{
		var childs = obj.childNodes;
		while(childs.length)
		{
			obj.removeChild(childs[0]);
		}
	}
} 

 //*************************
var g_ItemConfigArray = new Array();	//项配置信息数组
//增加项配置
function AddItemConfig(sname, ntype, pfunc, sexdata, sexdata2, sexdata3)
{
	var item = new ItemConfig(sname, ntype, pfunc, sexdata, sexdata2, sexdata3);
	g_ItemConfigArray.push(item);
}
//清空配置
function CleanConfig()
{
    g_ItemConfigArray.length = 0;
}

 //*************************************
function ItemConfig(sname, ntype, pfunc, exdata, exdata2, exdata3)
{   
	if(ntype == null) ntype = 0;
	this.name = sname;							//字段名
	this.type = ntype;							//类型 0
	this.func = pfunc;                          //事件处理函数
	this.exdata = exdata;                       //扩展
	this.exdata2 = exdata2;                     //扩展2
	this.exdata3 = exdata3;                     //扩展3
}
//---name----type-------func----------------exdata----exdata2----exdata3
//--字段名---0----------null------------------null----------------------显示数据库字段
//--字段名---1------方法名(index,字段值)------null----------------------<a>字段值</a>
//--字段名---2------方法名(index,字段值)-----显示str--------------------<a>显示str</a>
//--str------3----------null------------------null-----------------------显示str


//--字段1---97----方法名(index,字段1, ex2)--显示文字----ex2--------------<a>显示文字</a>
//--字段名--98-----方法对象A(index,字段值)---null----------------------显示方法A返回的字符串
//--字段名---99-----字符串------------------显示str--------------------<a href="字符串">显示str</a>
//--字段名---199-----字符串------------------字段2--------------------<a href="字符串">字段2</a>
//--字段1---100-----字段2---------------------ex1---------ex2-----------<a href="ex1+ 字段1+字段2+ex2">字段1+ex2</a>
//--字段1---101-----字段2---------------------ex1---------ex2-----ex3---<a href="ex1+ 字段1+字段2+_ex3 +ex2">字段2+_ex3 +ex2</a>


//--字段1---201------字段2------------------文字1----------------------<input type="text" id="字段1值_文字1" name="字段1值_文字1" value=字段2 />
//--字段1---211------字段2------------------文字1----------字段3------------<input type="text" id="字段1值_文字1" name="字段1值_文字1" value=字段2或者字段3 />
//--字段1---202------tmpValue---------------文字1---------文字2-------------<input type="checkbox" id="字段1值_文字1" name="字段1值_文字1" value=tmpValue />文字2

//--字段1--298-----方法对象A(index,字段1值,字段2值,字段3值)---字段2----------字段3----exdata3---<a href="显示方法A返回的字符串">exdata3</a>

